#include "MapCell.h"
#include "../Managers/GameManager.h"
#include "TerrainType.h"

MapCell::MapCell(const Vec2i &pos, float altitude, float slope, const Vec3f &normal)
	: pos(pos), altitude(altitude), normal(normal), slope(slope), walkable(true), can_plant_tree(true)
{
	terrainType = GameManager::getSingleton()->getTerrainTypeMgr()->getTerrainType(TerrainId::TERRAIN_GROUND);
}

MapCell::MapCell(Vec2i &&pos, float altitude, float slope, Vec3f &&normal)
	: pos(pos), altitude(altitude), normal(normal), slope(slope), walkable(true), can_plant_tree(true)
{
	terrainType = GameManager::getSingleton()->getTerrainTypeMgr()->getTerrainType(TerrainId::TERRAIN_GROUND);
}

MapCell::MapCell(int x, int y, float altitude, float slope, const Vec3f &normal)
	: pos(x, y), altitude(altitude), normal(normal), slope(slope), walkable(true), can_plant_tree(true)
{
	TerrainId type;
	if(altitude < -1.0f)
		type = TERRAIN_DEEP_WATER;
	else if(altitude < 0.0f)
		type = TERRAIN_SHALLOW_WATER;
	else if(altitude < 40.0f && abs(slope) < 0.4f)
		type = TERRAIN_GRASS_LUSH;
	else if(altitude < 100.0f && abs(slope) < 0.4f)
		type = TERRAIN_GRASS_NORMAL;
	else if(altitude < 150.0f && abs(slope) < 0.4f)
		type = TERRAIN_GRASS_SPARSE;
	else if(altitude >= 150.0f && abs(slope) < 0.4f)
		type = TERRAIN_SNOW_DEEP;
	else if(altitude < 140.0f && abs(slope) < 0.5f)
		type = TERRAIN_GROUND;
	else if(altitude <= 180.0f && abs(slope) < 0.5f)
		type = TERRAIN_GROUND_FROZEN;
	else if(altitude > 180.0f && abs(slope) < 0.1f)
		type = TERRAIN_SNOW_NORMAL;
	else if(altitude > 180.0f && abs(slope) < 0.5f)
		type = TERRAIN_SNOW_SHALLOW;
	else if(abs(slope) >= 0.5f)
		type = TERRAIN_ROCK_BARE;
	else
		type = TERRAIN_ROCK_DIRTY;

	terrainType = GameManager::getSingleton()->getTerrainTypeMgr()->getTerrainType(type);
	walkable = terrainType->walkable;
}

MapCell::~MapCell()
{
}